package top.codedance.iotp.registy.net.handle.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.common.entity.RegistServerMeta;
import top.codedance.iotp.common.entity.ServerMeta;
import top.codedance.iotp.registy.RegistryApplication;
import top.codedance.iotp.registy.net.handle.AdapterController;

import java.util.Map;
import java.util.Set;

public class QueryServerListController extends AdapterController {

    @Override
    public String doSubHandler(JSONObject body) {
        Map<String, ServerMeta> servers = RegistryApplication.getServers();
        JSONArray jsonArray = new JSONArray();
        Set<Map.Entry<String, ServerMeta>> entries = servers.entrySet();
        for(Map.Entry<String, ServerMeta> entry : entries){
            RegistServerMeta rsm = new RegistServerMeta(entry.getValue());
            rsm.setClientCount(RegistryApplication.getClientCountForServerId(rsm.getBindIp() + ":" + rsm.getTcpPort()));
            jsonArray.add(rsm.getJSONObject());
        }
        return okResponse(jsonArray);
    }
}
